package com.bbs.controller;

import com.bbs.resp.ResultMsg;
import com.bbs.service.HdfsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.fs.BlockLocation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author: ZackYoung
 * @time: 2020/11/13
 */
@RestController
@RequestMapping("/hadoop/hdfs")
public class HdfsController {

    /**
     * 创建文件夹
     *
     * @param path
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "mkdir", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg mkdir(@RequestParam("path") String path) throws Exception {
        if (StringUtils.isEmpty(path)) {
            return new ResultMsg(ResultMsg.FAILURE, "请求参数为空");
        }
        // 创建空文件夹
        boolean isOk = HdfsService.mkdir(path);
        if (isOk) {
            return new ResultMsg(ResultMsg.SUCCESS, "文件夹创建成功");
        } else {
            return new ResultMsg(ResultMsg.FAILURE, "文件夹创建失败");
        }
    }

    /**
     * 读取HDFS目录信息
     *
     * @param path
     * @return
     * @throws Exception
     */
    @PostMapping("/readPathInfo")
    public ResultMsg readPathInfo(@RequestParam("path") String path) throws Exception {
        List<Map<String, Object>> list = HdfsService.readPathInfo(path);
        return new ResultMsg(ResultMsg.SUCCESS, "读取HDFS目录信息成功", list);
    }

    /**
     * 获取HDFS文件在集群中的位置
     *
     * @param path
     * @return
     * @throws Exception
     */
    @PostMapping("/getFileBlockLocations")
    public ResultMsg getFileBlockLocations(@RequestParam("path") String path) throws Exception {
        BlockLocation[] blockLocations = HdfsService.getFileBlockLocations(path);
        return new ResultMsg(ResultMsg.SUCCESS, "获取HDFS文件在集群中的位置", blockLocations);
    }

    /**
     * 创建文件
     *
     * @param path
     * @return
     * @throws Exception
     */
    @PostMapping("/createFile")
    public ResultMsg createFile(@RequestParam("path") String path, @RequestParam("file") MultipartFile file)
            throws Exception {
        if (StringUtils.isEmpty(path) || null == file.getBytes()) {
            return new ResultMsg(ResultMsg.FAILURE, "请求参数为空");
        }
        HdfsService.createFile(path, file);
        return new ResultMsg(ResultMsg.SUCCESS, "创建文件成功");
    }

    /**
     * 读取HDFS文件内容
     *
     * @param path
     * @return
     * @throws Exception
     */
    @RequestMapping("/readFile")
    public String readFile(@RequestParam("path") String path) throws Exception {
        String s = HdfsService.readFile(path);
        return s;
    }

    /**
     * 读取HDFS图片
     *
     * @param path
     * @return
     * @throws Exception
     */

    @Resource
    private HttpServletResponse response;

    @RequestMapping(value = "/readImg", produces = MediaType.IMAGE_JPEG_VALUE)
    public void readImg(@RequestParam("path") String path) throws Exception {
        OutputStream os = null;
        try {
//        读取图片
            BufferedImage image = HdfsService.readImg(path);
            response.setContentType("image/png");
            os = response.getOutputStream();
            if (image != null) {
                ImageIO.write(image, "png", os);
            }
        } catch (IOException e) {
            System.out.println("获取图片异常{}" + e.getMessage());
        } finally {
            if (os != null) {
                os.flush();
                os.close();
            }
        }
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "uploadImage", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg upload(@RequestParam("file") MultipartFile file) throws Exception {
        HdfsService.uploadImage(file, "test");
        return new ResultMsg(ResultMsg.SUCCESS, "成功");
    }

}
